package com.cskaoyan.servlet;

import com.cskaoyan.bean.MarketLog;
import com.cskaoyan.bean.common.CommonData;
import com.cskaoyan.service.AdminLogService;
import com.cskaoyan.service.AdminLogServiceImpl;
import com.cskaoyan.util.ResponseUtil;
import com.cskaoyan.util.StringUtil;
import com.cskaoyan.util.URIUtil;
import lombok.SneakyThrows;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * @Author：LiuFan
 * @Project：first-project
 * @Package：com.cskaoyan.servlet
 * @Date：2023/5/31 15:12
 */

@WebServlet("/admin/log/*")
public class AdminLogServlet extends HttpServlet {

    private AdminLogService logService = new AdminLogServiceImpl();

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req,resp);
    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String operation = URIUtil.getOperation(this.getClass(), req);
        switch (operation){
            case "/list" :
                list(req,resp);
                break;
        }
    }

    @SneakyThrows
    private void list(HttpServletRequest req, HttpServletResponse resp) {
        String name = StringUtil.isEmpty(req.getParameter("name")) ? null : req.getParameter("name");
        Integer page = StringUtil.isNotEmpty(req.getParameter("page"))?Integer.parseInt(req.getParameter("page")):null;
        Integer limit = StringUtil.isNotEmpty(req.getParameter("limit"))?Integer.parseInt(req.getParameter("limit")):null;
        String sort = req.getParameter("sort");
        String order = req.getParameter("order");
        List<MarketLog> data = logService.list(name, page, limit, sort, order);
//        ResponseUtil.responseJson(resp, BaseRespVo.ok(CommonData.data(data)));
        ResponseUtil.responseOkJsonVo(resp, CommonData.data(data));
    }
}
